using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SpaceBattle.Graphics;
using SpaceBattle.Model;
using SpaceBattle.Utilities;
using SpaceBattle.Viev;

namespace SpaceBattle
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        public SpriteBatch cameraBatch;
        private Camera cam = Camera.instance;

        public GraphicsDeviceManager Graphics
        {
            get { return graphics; }
        }

        public SpriteBatch CameraBatch
        {
            get { return cameraBatch; }
        }

        private SpriteBatch solidBatch;

        public SpriteBatch SolidBatch
        {
            get { return solidBatch; }
        }

        #region Physics

        private List<Body> _physicBodies;
        public List<Body> PhysicBodies
        {
            get
            {
                return _physicBodies;
            }
        }

        public void addPhysicBody(Body body)
        {
            _physicBodies.Add(body);
        }

        public void removePhysicBody(Body body)
        {
            _physicBodies.Remove(body);
        }

        #endregion

        #region Constructor

        private static GameMain gameSingleton;

        private GameMain()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            _physicBodies = new List<Body>();
            IsFixedTimeStep = true;
            IsMouseVisible = true;

            Graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 300;
            Graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height - 300;
            //Graphics.IsFullScreen = true;
            ScrollWheelValue = Mouse.GetState().ScrollWheelValue;
        }

        public static GameMain Game()
        {
            if (gameSingleton == null)
            {
                gameSingleton = new GameMain();
            }

            return gameSingleton;
        }

        #endregion

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            cameraBatch = new SpriteBatch(GraphicsDevice);
            solidBatch = new SpriteBatch(GraphicsDevice);

            TextureLoader.Textures().LoadTextures(this);

            font = GameMain.Game().Content.Load<SpriteFont>("myFont");

            // TODO: use this.Content to load your game content here
        }

        private SpriteFont font;

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        private int ScrollWheelValue;

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            PhysicsCalculator.countForces(PhysicBodies);
            Input.Update(gameTime);
            cam._pos = planet1.Body.Position-new Vector2(graphics.PreferredBackBufferWidth/2,graphics.PreferredBackBufferHeight/2);
            cam.Rotation = MathHelper.ToRadians(90-planet1.Body.Rotation);

            if (Mouse.GetState().ScrollWheelValue > ScrollWheelValue)
                cam.Zoom+=.1f;
            if (Mouse.GetState().ScrollWheelValue < ScrollWheelValue)
                cam.Zoom-=.1f;

            ScrollWheelValue = Mouse.GetState().ScrollWheelValue;

            base.Update(gameTime);
        }

        private PhysicsSprite planet1;

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkSlateGray);

            // TODO: Add your drawing code here

            solidBatch.DrawString(font, gameTime.ElapsedGameTime.ToString(), new Vector2(100, 100), Color.White);

            base.Draw(gameTime);
        }

        protected override void BeginRun()
        {
            base.BeginRun();

            PhysicsSprite sun = new PhysicsSprite(new Body(2500, new Vector2(0, 0), 400, Color.Yellow), true, TextureLoader.Textures().sunTex);
            planet1 = new PhysicsSprite(new Body(100, new Vector2(200, 400), 70, Color.White), false, TextureLoader.Textures().shipTex);
            PhysicsSprite planet2 = new PhysicsSprite(new Body(50, new Vector2(600, 100), 72, Color.Red), false, TextureLoader.Textures().sunTex);
            PhysicsSprite planet3 = new PhysicsSprite(new Body(100, new Vector2(1400, 400), 12, Color.Blue), false, TextureLoader.Textures().sunTex);
            PhysicsSprite planet4 = new PhysicsSprite(new Body(80, new Vector2(1800, 100), 50, Color.Green), false, TextureLoader.Textures().sunTex);
            planet2.Body.Force = new Vector2(0, 1.5f);
            planet3.Body.Force = new Vector2(0, -1.5f);
            planet4.Body.Force = new Vector2(0, -1.5f);

            Radar radar = new Radar(PhysicBodies);
        }

        protected override void EndDraw()
        {
            solidBatch.End();
            cameraBatch.End();
            base.EndDraw();
        }



        protected override bool BeginDraw()
        {
            base.BeginDraw();
            cameraBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(Graphics));
            solidBatch.Begin();
            return true;


        }
    }
}
